package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.api.resolver.annotation.SsoTeacher;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.entity.UpdateUser;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.WxUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Api(description = "用户登录接口")
@RestController
public class UserController {

    @Reference
    private SsoService ssoService;

    @Reference
    private StudentService studentService;

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "登录信息", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @PostMapping("/login")
    public PtpResult login(String name, String password) {
        String token = ssoService.ssoUser(name, password);
        return token != null ? PtpResultUtils.ok(token) : PtpResultUtils.error(499, "登录失败，账号或密码错误");
    }

    @ApiOperation("微信小程序登录")
    @PostMapping("/wx/login")
    public PtpResult wxLogin(@RequestBody WxUser wxUser) {
        String token = ssoService.ssoUser(wxUser.getName(), wxUser.getPassword());
        if (token == null) {
            return PtpResultUtils.error(499, "登录失败，账号或密码错误");
        }
        boolean success = true;
        String code = wxUser.getCode();
        String head = wxUser.getHead();
        if (code != null || head != null) {
            Object user = ssoService.tokenUser(token);
            if (user instanceof Student) {
                Student student = (Student) user;
                if (head != null) {
                    student.setHead(head);
                }
                success = studentService.completeStudent(student, code);
            }
        }
        return success ? PtpResultUtils.ok(token) : PtpResultUtils.error(499, "登录失败，完善学生信息错误");
    }

    @ApiOperation("登出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header")
    })
    @GetMapping("/logout")
    public PtpResult logout(@RequestHeader String token) {
        boolean success = ssoService.logout(token);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(498, "登出失败");
    }

    @ApiOperation("通过token获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header")
    })
    @GetMapping("/user")
    public PtpResult getUserByToken(@RequestHeader String token) {
        Object user = ssoService.tokenUser(token);
        return user != null ? PtpResultUtils.ok(user) : PtpResultUtils.error(490, "token无效");
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header"),

    })
    @PutMapping("/user/update")
    public PtpResult updateUser(@ApiIgnore @SsoStudent Student student,
                                @RequestBody UpdateUser updateUser,
                                @ApiIgnore @SsoTeacher Teacher teacher
    ) {
        Boolean success = null;

        if (teacher.getUser() != null) {
            success = ssoService.changePass(updateUser, teacher.getUser().getTel(), teacher.getUser().getNo());
        } else {
            success = ssoService.changePass(updateUser, student.getUser().getTel(), student.getUser().getNo());
        }


        return success ? PtpResultUtils.ok(success) : PtpResultUtils.error(499, "修改密码失败");
    }


}
